﻿#include "connetionsttings.h"
#include "ui_connetionsttings.h"
#include <qserialportinfo.h>
#include <QDebug>

ConnetionSttings::ConnetionSttings(QWidget* parent)
    : QDialog(parent)
    , ui(new Ui::ConnetionSttings)
{
    ui->setupUi(this);

    //串口连接
    connect(ui->applyButton, &QPushButton::clicked, this, &ConnetionSttings::applySetting2Connect);
    connect(ui->stopButton, &QPushButton::clicked, this, &ConnetionSttings::StopConnnection);
    connect(ui->refreshButton, &QPushButton::clicked, this, &ConnetionSttings::refreshPortLists);

    //串口数据初始化
    populatePorts();
    populateBaudRates();
    populateDataBits();
    populateParity();
    populateStopBits();
    populateResponseTime();
}


ConnetionSttings::~ConnetionSttings()
{
    delete ui;
}


//以下都为串口设置的函数
void ConnetionSttings::populatePorts()
{
    ui->comComb->clear();
    QStringList portList;
    foreach (const QSerialPortInfo& info, QSerialPortInfo::availablePorts())
    {
        portList.append(info.portName());
    }
    ui->comComb->addItems(portList);
}

void ConnetionSttings::refreshPortLists()
{
    populatePorts();
}

void ConnetionSttings::populateBaudRates()
{
    QList<int> baudRates = {9600, 19200, 38400, 57600, 115200};
    for (int rate : baudRates)
    {
        ui->brComb->addItem(QString::number(rate) + " bps", rate);
    }
    ui->brComb->setCurrentIndex(0);//默认9600
}

void ConnetionSttings::populateDataBits()
{

    ui->dbComb->addItem("7", QSerialPort::Data7);
    ui->dbComb->addItem("8", QSerialPort::Data8);
    ui->dbComb->setCurrentIndex(1); // 默认选中 8
}

void ConnetionSttings::populateStopBits()
{
    ui->sbComb->addItem("1", QSerialPort::OneStop);
    ui->sbComb->addItem("1.5", QSerialPort::OneAndHalfStop);
    ui->sbComb->addItem("2", QSerialPort::TwoStop);
    ui->sbComb->setCurrentIndex(2); // 默认 2
}

void ConnetionSttings::populateParity()
{
    ui->parityComb->addItem("None", QSerialPort::NoParity);
    ui->parityComb->addItem("Even", QSerialPort::EvenParity);
    ui->parityComb->addItem("Odd", QSerialPort::OddParity);
    ui->parityComb->setCurrentIndex(0);//默认无校验
}

void ConnetionSttings::populateResponseTime()
{
    ui->rtSpinbox->setSuffix(" ms");
    ui->rtSpinbox->setValue(50);
}

//将上面设置好的参数存入portsetting这个struct中进行发送
void ConnetionSttings::applySetting2Connect()
{
    portSetting.portName = ui->comComb->currentText();
    portSetting.boudRate = static_cast<QSerialPort::BaudRate>(ui->brComb->currentData().toInt());
    portSetting.dataBits = static_cast<QSerialPort::DataBits>(ui->dbComb->currentData().toInt());
    portSetting.stopBoits = static_cast<QSerialPort::StopBits>(ui->sbComb->currentData().toInt());
    portSetting.parity = static_cast<QSerialPort::Parity>(ui->parityComb->currentData().toInt());
    portSetting.responseTime = ui->rtSpinbox->value();


    qDebug() << "BaudRate:" << portSetting.boudRate;
    emit signalSerialConfigUpdatad(portSetting);
    qDebug() << "串口信息已发送！！";
}

//发送停止串口的信号
void ConnetionSttings::StopConnnection()
{
    emit signalSerialConfigStop();
    qDebug() << " 停止信息已发送";
}





